<template>
  <div class="app-container">
    <!-- 条件查询 -->
    <el-card class="box-card">
      <el-form :inline="true" :model="query" size="mini">
        <el-form-item label="用户ID">
          <el-input v-model.trim="query.userId" clearable placeholder="请输入用户ID"/>
        </el-form-item>
        <!-- 状态（ 0 启用；1 待用；2 停用 ） -->
        <el-form-item label="状态">
          <el-select v-model="query.status" clearable style="width: 120px">
            <el-option label="启用" :value="0"/>
            <el-option label="待用" :value="1"/>
            <el-option label="停用" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="query.begin"
            clearable
            placeholder="开始时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="-">
          <el-date-picker
            v-model="query.end"
            clearable
            placeholder="结束时间"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-button v-permission="'background:search'" icon="el-icon-search" type="primary" @click="queryData">查询
          </el-button>
          <el-button v-permission="'background:search'" icon="el-icon-refresh" @click="reload">重置</el-button>
          <el-button v-permission="'background:add'" type="success" icon="el-icon-plus" @click="openAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格显示列表数据 -->
    <el-table
      :data="list"
      stripe
      border
    >
      <el-table-column label="序号" width="70" fixed align="center">
        <template slot-scope="scope">
          <!--  page.current 当前页码；page.size 每页记录数 -->
          {{ (current - 1) * size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" align="center" width="200"/>
      <el-table-column prop="userId" label="用户ID" align="center" width="200"/>
      <el-table-column align="center" prop="url" label="背景图片" width="100">
        <template slot-scope="scope">
          <el-image
            :src="scope.row.url"
            :preview-src-list="[scope.row.url]"
          />
        </template>
      </el-table-column>
      <!-- 状态（ 0 启用；1 待用；2 停用 ） -->
      <el-table-column align="center" prop="status" label="状态" width="130">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="">启用</el-tag>
          <el-tag v-if="scope.row.status == 1" type="success">待用</el-tag>
          <el-tag v-if="scope.row.status == 2" type="info">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="updateTime" label="更新时间" width="160px"/>
      <el-table-column align="center" prop="createTime" label="创建时间" width="160px"/>
      <el-table-column align="center" label="操作" width="250px" fixed="right">
        <template slot-scope="scope">
          <!-- 1、打开审核窗口，只有 status == 0 待审核状态，才显示审核按钮  v-permission="'article:audit'" -->
          <el-button
            v-permission="'background:edit'"
            :disabled="scope.row.status == 0"
            size="mini"
            type="primary"
            @click="working(scope.row.id)"
          >启用
          </el-button>
          <el-button
            v-permission="'background:edit'"
            :disabled="scope.row.status == 2"
            size="mini"
            type="info"
            @click="stop(scope.row.id)"
          >停用
          </el-button>
          <el-button v-permission="'background:delete'" type="danger" size="mini" @click="handleDelete(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      align="center"
      background
      :current-page="current"
      :page-sizes="[10, 20,30,50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 子组件-新增首页轮播图背景图片 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleClose"
    >
      <h1><i class="el-icon-circle-plus"/> 新增 - 轮播图背景图片</h1>
      <add :form-data="formData" @handleClose="handleClose" @fetchData="fetchData"/>
    </el-dialog>
  </div>
</template>
<script>
import api from '@/api/background'
import Add from './add'

export default {
  name: 'Background',
  components: {
    Add
  },
  data() {
    return {
      list: [],
      current: 1,
      size: 10,
      total: 0,
      query: {},
      // 新增
      dialogVisible: false,
      formData: {
        url: ''
      }
    }
  },
  created() {
    // 数据列表
    this.fetchData()
  },
  methods: {
    // 打开新增背景图片对话框
    openAdd() {
      this.formData.url = ''
      this.dialogVisible = true
    },
    // 关闭新增窗口
    handleClose() {
      this.dialogVisible = false
    },
    // 获取条件分页列表数据
    async fetchData() {
      const { data } = await api.getList(this.current, this.size, this.query)
      this.list = data.records
      this.total = Number(data.total)
    },
    // 切换每页显示多少条记录
    handleSizeChange(size) {
      this.size = size
      this.fetchData()
    },
    // 切换当前页码
    handleCurrentChange(current) {
      this.current = current
      this.fetchData()
    },
    // 条件查询
    queryData() {
      this.current = 1
      this.fetchData()
    },
    // 重置表单
    reload() {
      this.query = {}
      this.fetchData()
    },
    // 启用
    working(id) {
      this.$confirm('确定要 启用 此背景图片吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送请求
        api.working(id).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message.success('启用背景图片成功')
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    // 停用
    stop(id) {
      this.$confirm('确定要 停用 此背景图片吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送请求
        api.stop(id).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message.success('启用背景图片成功')
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    // 根据ID删除
    handleDelete(id) {
      this.$confirm('确定要 删除 这条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发送请求
        api.deleteById(id).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message.success(response.message)
          } else {
            this.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
<style scoped>
.el-image {
  border-radius: 4px;
}

.el-pagination {
  margin: 20px 0 50px;
}

h1 {
  text-align: center;
}
</style>
